document.getElementById('my_img_id').addEventListener('tap', uploadImages, false);
document.getElementById('my_img_id2').addEventListener('tap', uploadImages, false);
document.getElementById('my_img_id3').addEventListener('tap', uploadImages, false);
document.getElementById('my_img_id4').addEventListener('tap', uploadImages, false);

function uploadImages() {
	var _this = $(this);
	if(mui.os.plus) {
		var a = [{
			title: "拍照"
		}, {
			title: "从手机相册选择"
		}];
		plus.nativeUI.actionSheet({
			title: "上传图片",
			cancel: "取消",
			buttons: a
		}, function(b) { /*actionSheet 按钮点击事件*/
			switch(b.index) {
				case 0:
					break;
				case 1:
					getImage(_this); /*拍照*/
					break;
				case 2:
					galleryImg(_this); /*打开相册*/
					break;
				default:
					break;
			}
		})
	}
}

//拍照 
function getImage(obj) {

	var c = plus.camera.getCamera();
	c.captureImage(function(e) {
		plus.io.resolveLocalFileSystemURL(e, function(entry) {
			var s = entry.toLocalURL() + "?version=" + new Date().getTime();
			uploadHead(s, obj); /*上传图片*/
		}, function(e) {
			console.log("读取拍照文件错误：" + e.message);
		});
	}, function(s) {
		console.log("error" + s);
	}, {
		filename: "_doc/head.png"
	})
}

//本地相册选择 
function galleryImg(obj) {
	plus.gallery.pick(function(a) {
		plus.io.resolveLocalFileSystemURL(a, function(entry) {
			plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
				root.getFile("head.png", {}, function(file) {
					//文件已存在 
					file.remove(function() {
						console.log("file remove success");
						entry.copyTo(root, 'head.png', function(e) {
								var e = e.fullPath + "?version=" + new Date().getTime();
								uploadHead(e, obj); /*上传图片*/
								//变更大图预览的src 
								//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现 
							},
							function(e) {
								console.log('copy image fail:' + e.message);
							});
					}, function() {
						console.log("delete image fail:" + e.message);
					});
				}, function() {
					//文件不存在 
					entry.copyTo(root, 'head.png', function(e) {
							var path = e.fullPath + "?version=" + new Date().getTime();
							uploadHead(path, obj); /*上传图片*/
						},
						function(e) {
							console.log('copy image fail:' + e.message);
						});
				});
			}, function(e) {
				console.log("get _www folder fail");
			})
		}, function(e) {
			console.log("读取拍照文件错误：" + e.message);
		});
	}, function(a) {}, {
		filter: "image"
	})
};

//上传头像图片 
var width = 0; 
var height = 0;
function uploadHead(imgPath, obj) {
	var _this = obj;
	console.log("imgPath = " + imgPath);

	_this.attr('src', imgPath);
	// _this.src = imgPath; 
	// _this.style.width = "99px"; 
	//_this.style.height = "99px";  
//	var width = _this.attr('width') ? _this.attr('width') : 120;
//	var height = _this.attr('height') ? _this.attr('height') : 150;
	var image = new Image();
	image.src = imgPath;
	image.onload = function() {
		width=400;
    	height=400*image.height/image.width;
		var imgData = getBase64Image(image, width, height);
		
		console.log(imgData);
		/*在这里调用上传接口*/
		mui.ajax(H_SRC + "/amier/user/uploadBase64Images", {
			data: {
				'imgBase64': imgData
			},
			dataType: 'json',
			type: 'post',
			timeout: 10000,
			success: function(data) {
				if(data.code==1) {
					mui.toast('上传成功');
					_this.prev().val(data.src);
					_this.attr("src",H_SRC+data.src);

				} else {
					mui.toast('上传失败！请重试。。。');
				}
			},
			error: function(xhr, type, errorThrown) {
				mui.toast('网络异常，请稍后再试！');
			}
		});
	}
}
//将图片压缩转成base64 
function getBase64Image(img, width, height) {
	var canvas = document.createElement("canvas");
	var width = width;
	var height = height;
	//压缩图片
	// calculate the width and height, constraining the proportions 
	/*if (width > height) { 
        if (width > 800) { 
            height = Math.round(height *= 800 / width); 
            width = 800; 
        } 
    } else { 
        if (height > 800) { 
            width = Math.round(width *= 800 / height); 
            height = 800; 
        } 
    }*/
	canvas.width = width; /*设置新的图片的宽度*/
	canvas.height = height; /*设置新的图片的长度*/
	var ctx = canvas.getContext("2d");
	ctx.drawImage(img, 0, 0, width, height); /*绘图*/
	var dataURL = canvas.toDataURL("image/png", 0.8);
	//return dataURL.replace("data:image/png;base64,", "");
	return dataURL;
}